package com.example.qzzm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.adapter.MyAdapter;
import com.example.application.QZApplication;
import com.example.constant.App;
import com.example.jx.FileUtils;
import com.example.jx.JsonUtils;
import com.example.net.Client;
import com.example.qzzm.R;
import com.example.vo.Act;
import com.example.vo.Message;
import com.xlistview.maxwin.view.XListView;
import com.xlistview.maxwin.view.XListView.IXListViewListener;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

/**
 * 主界面的Activity
 * 
 * @author Administrator
 *
 */
public class ActActivity extends Activity {
	// 登录按钮
	private ImageView ivBack;
	// 列表数据
	private List<Act> acts = null;
	// 列表视图
	private XListView mXListView;
	// 数据适配器
	private MyAdapter mAdapter;
	// 全局的Handler
	private Handler mHandler;
	// 页码
	private int page = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_act);
		mHandler = new Handler();
		acts = new ArrayList<Act>();

		initView();
		initListeners();

		readChache();
	}

	/**
	 * 读取缓存
	 */
	private void readChache() {
		Serializable serializable = FileUtils.readObjectFromChache("list_ActActivity_json");
		if(serializable != null){
			List<Act> temps = new ArrayList<Act>();
			temps = JsonUtils.paraseJson(serializable.toString(), Message.class).getLists();
			for (Act act : temps) {
				acts.add(act);
			}
			mAdapter = new MyAdapter(ActActivity.this, acts);
			mXListView.setAdapter(mAdapter);
		}else{
			FirstTask fTask = new FirstTask();
			fTask.execute(page);
		}
	}

	// 初始化布局控件
	private void initView() {
		ivBack = (ImageView) findViewById(R.id.iv_back);
		mXListView = (XListView) findViewById(R.id.mlv_list);
		mXListView.setPullLoadEnable(true);
	}

	// 初始化监听事件
	private void initListeners() {
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mXListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(ActActivity.this, XQActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("itemId",
						acts.get((int) arg0.getAdapter().getItemId(arg2))
								.getAct_id());
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});

		mXListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {

						FirstTask fTask = new FirstTask();
						fTask.execute(page);
						onLoad();
					}
				}, 2000);
			}

			@Override
			public void onLoadMore() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						page++;
						FirstTask fTask = new FirstTask();
						fTask.execute(page);
					}
				}, 2000);
			}
		});
	}

	class FirstTask extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			// 开始执行
			// 网络访问，获取列表数据
			QZApplication application = (QZApplication) getApplication();
			Client client = application.getClient();
			String result = client.getResquest(App.ORG_URL + "city_id="
					+ App.cityNo.get(application.getCity()) + "&page="
					+ params[0]);
			List<Act> temps = new ArrayList<Act>();
			if (result != null) {
				temps = JsonUtils.paraseJson(result, Message.class).getLists();
				if(page == 1){
					//缓存第一页内容
					FileUtils.saveObjectToChache(result,"list_ActActivity_json");
				}
			}
			for (Act act : temps) {
				acts.add(act);
			}
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) {
			// 执行成功
			if (acts == null) {
				return;
			}

			// 首次初始化
			if (mXListView.getAdapter() == null) {
				mAdapter = new MyAdapter(ActActivity.this, acts);
				mXListView.setAdapter(mAdapter);
			}
			onLoad();
			mAdapter.notifyDataSetChanged();

			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			// 更新
			super.onProgressUpdate(values);
		}

	}

	// 下拉刷新结束
	private void onLoad() {
		mXListView.stopRefresh();
		mXListView.stopLoadMore();
	}
}
